﻿<%@ WebHandler Language="C#" Class="Course" %>

using System;
using System.Web;
using System.Collections.Generic;
using Company.StudyClub.Model;
using Company.StudyClub.BLL.Course;
using Company.StudyClub.BLL.Common;
using Company.StudyClub.BLL.Tag;
using System.Text;

public class Course : IHttpHandler
{
    private HttpHelper helper = new HttpHelper();

    public void ProcessRequest(HttpContext context)
    {
        HttpResponse response = context.Response;

        switch (helper.GetString("type"))
        {            
            case "getcatalogs":
                GetCatalogs(response);
                break;           
            case "checkserialnumber":
                CheckSerialNumber(response);
                break;
            case "getcourseinfo":
                GetCourseInfo(response);
                break;
            case "searchcourse":
                SearchCourses(response);
                break;
            case "coursenum":
                GetTotalCourses(response);
                break;
            case "createfilename":
                CreateFileName(response);
                break;
            case "getcoursesection":
                GetCourseSection(response);
                break;
            case "getcoursetags":
                GetCourseTags(response);
                break;
            default: break;
        }
    }

    private void GetCatalogs(HttpResponse response)
    {
        CatalogBLL objBLL = new CatalogBLL();

        List<Course_Catalog> objList = objBLL.GetCatalogsByParentID(helper.GetInt("pid"));

        StringBuilder objSb = new StringBuilder();

        objSb.Append("[");
        for (int i = 0; i < objList.Count; i++)
        {
            Course_Catalog model = (Course_Catalog)objList[i];

            objSb.Append("{\"cid\":\"");
            objSb.Append(model.CatalogID);
            objSb.Append("\",\"cname\":\"");
            objSb.Append(model.CatalogName);
            objSb.Append("\",\"totalsubs\":\"");
            objSb.Append(model.TotalSubs);
            objSb.Append("\",\"totalcourses\":\"");
            objSb.Append(model.TotalCourses);
            objSb.Append("\",\"sequence\":\"");
            objSb.Append(model.Sequence);
            objSb.Append("\",\"pid\":\"");
            objSb.Append(model.ParentID);
            objSb.Append("\",\"ctime\":\"");
            objSb.Append(model.CreateTime);
            objSb.Append("\"}");
            if (i != objList.Count - 1)
            {
                objSb.Append(",");
            }
        }
        objSb.Append("]");

        response.Write(objSb.ToString());
    }
    
    private void CheckSerialNumber(HttpResponse response)
    {
        CourseBLL objBLL = new CourseBLL();

        bool result = objBLL.CheckSerialNumber(helper.GetString("serialnumber"));

        if (result)
        {
            response.Write("false");
        }
        else
        {
            response.Write("true");
        }
    }

    private void GetCourseInfo(HttpResponse response)
    {
        CourseBLL objBLL = new CourseBLL();
        Course_Info model = objBLL.GetModel(helper.GetInt("courseid"));
        int isedit = helper.GetInt("isedit");

        string catalogname = model.CatalogName;
        string playlength = (model.PlayLength / 60).ToString();

        if (isedit == 0)
        {
            CatalogBLL objCatalogBLL = new CatalogBLL();
            catalogname = objCatalogBLL.GetFullCatalogName(model.CatalogID);

            playlength = Utility.TimeSwitch(model.PlayLength);   
        }
        
        StringBuilder objSb = new StringBuilder();
        objSb.Append("{");
        if (model != null)
        {
            objSb.Append("\"coursename\":\"");
            objSb.Append(model.CourseName);
            objSb.Append("\",\"courseid\":\"");
            objSb.Append(model.CourseID);
            objSb.Append("\",\"coursetype\":\"");           
            objSb.Append(model.CourseType);
            objSb.Append("\",\"coursetypename\":\"");
            objSb.Append(Utility.GetCourseType(model.CourseType));         
            objSb.Append("\",\"serialnumber\":\"");
            objSb.Append(model.SerialNumber);
            objSb.Append("\",\"schoolid\":\"");
            objSb.Append(model.SchoolID);            
            objSb.Append("\",\"covericon\":\"");
            objSb.Append(model.CoverIcon);            
            objSb.Append("\",\"catalogname\":\"");
            objSb.Append(catalogname);
            objSb.Append("\",\"catalogid\":\"");
            objSb.Append(model.CatalogID);
            objSb.Append("\",\"playlength\":\"");
            objSb.Append(playlength);
            objSb.Append("\",\"description\":\"");
            objSb.Append(Utility.TextEncoding(model.Description));
            objSb.Append("\",\"lecturerintro\":\"");
            objSb.Append(Utility.TextEncoding(model.LecturerIntro));
            objSb.Append("\",\"createtime\":\"");
            objSb.Append(model.CreateTime.ToString("yyyy-MM-dd"));
            objSb.Append("\",\"keywords\":\"");
            objSb.Append(model.Keywords);
            objSb.Append("\"");
        }
        objSb.Append("}");
        
        response.Write(objSb.ToString());
    }

    private void GetTotalCourses(HttpResponse response)
    {
        string coursename = helper.GetString("coursename");
        int catalogid = helper.GetInt("catalogid");
        int includesub = helper.GetInt("includesub");
        int coursetype = helper.GetInt("ctype");
        int status = helper.GetInt("status");
        string serialnumber = helper.GetString("serialnumber");
        int isdelete = helper.GetInt("isdelete");

        CourseBLL objBLL = new CourseBLL();

        response.Write(objBLL.GetTotalUsers(helper.CurrentUser.SchoolID, coursename, serialnumber, coursetype, catalogid, includesub, status, -1, isdelete));
    }
    
    private void SearchCourses(HttpResponse response)
    {
        int pageIndex = helper.GetInt("pageindex");
        int pageSize = helper.GetInt("pagesize");
        string coursename = helper.GetString("coursename");
        int catalogid = helper.GetInt("catalogid");
        int includesub = helper.GetInt("includesub");
        int coursetype = helper.GetInt("ctype");
        int status = helper.GetInt("status");
        string serialnumber = helper.GetString("serialnumber");
        int isdelete = helper.GetInt("isdelete");
            
        CourseBLL objBLL = new CourseBLL();

        List<Course_Info> objList = objBLL.SearchCourses(pageIndex, pageSize, helper.CurrentUser.SchoolID, coursetype, coursename, serialnumber, catalogid, includesub, status, -1, isdelete);

        StringBuilder objSb = new StringBuilder();

        objSb.Append("[");
        for (int i = 0; i < objList.Count; i++)
        {
            Course_Info model = (Course_Info)objList[i];
            objSb.Append("{");
            objSb.Append("\"coursename\":\"");
            objSb.Append(model.CourseName);
            objSb.Append("\",\"courseid\":\"");
            objSb.Append(model.CourseID);
            objSb.Append("\",\"coursetype\":\"");
            objSb.Append(model.CourseType);
            objSb.Append("\",\"coursetypename\":\"");
            objSb.Append(Utility.GetCourseType(model.CourseType));            
            objSb.Append("\",\"serialnumber\":\"");
            objSb.Append(model.SerialNumber);
            objSb.Append("\",\"covericon\":\"");
            objSb.Append(model.CoverIcon);            
            objSb.Append("\",\"catalogname\":\"");
            objSb.Append(model.CatalogName);
            objSb.Append("\",\"playlength\":\"");
            objSb.Append(Utility.TimeSwitch(model.PlayLength));
            objSb.Append("\",\"description\":\"");
            objSb.Append(Utility.TextEncoding(model.Description));
            objSb.Append("\",\"lecturerintro\":\"");
            objSb.Append(Utility.TextEncoding(model.LecturerIntro));
            objSb.Append("\",\"createtime\":\"");
            objSb.Append(model.CreateTime.ToString("yyyy-MM-dd"));
            objSb.Append("\",\"row\":\"");
            objSb.Append(model.RowNumber);
            objSb.Append("\"}");

            if (i != objList.Count - 1)
                objSb.Append(",");
        }
        objSb.Append("]");

        response.Write(objSb.ToString());
    }

    private void CreateFileName(HttpResponse response)
    {
        response.Write(DateTime.Now.ToString("yyyyMMddHHmmssfff") + "_S");
    }

    private void GetCourseSection(HttpResponse response)
    {
        SectionBLL objBLL = new SectionBLL();

        List<Course_Section> objList = objBLL.GetCourseSections(helper.GetInt("courseid"));

        StringBuilder objSb = new StringBuilder();

        objSb.Append("[");

        for (int i = 0; i < objList.Count; i++)
        {
            Course_Section model = (Course_Section)objList[i];
            
            objSb.Append("{");
            objSb.Append("\"sectionid\":\"");
            objSb.Append(model.SectionID);
            objSb.Append("\",\"courseid\":\"");
            objSb.Append(model.CourseID);
            objSb.Append("\",\"title\":\"");
            objSb.Append(model.Title);
            objSb.Append("\",\"content\":\"");
            objSb.Append(model.Content);
            objSb.Append("\",\"href\":\"");
            objSb.Append(model.Href);
            objSb.Append("\",\"sequence\":\"");
            objSb.Append(model.Sequence);
            objSb.Append("\",\"levelid\":\"");
            objSb.Append(model.LevelID);
            objSb.Append("\",\"parent\":\"");
            objSb.Append(model.Parent);
            objSb.Append("\",\"indentifier\":\"");
            objSb.Append(model.ItemIndetifier);
            objSb.Append("\",\"type\":\"");
            objSb.Append(model.Type);
            objSb.Append("\"}");

            if (i != objList.Count - 1)
                objSb.Append(",");
        }

        objSb.Append("]");

        response.Write(objSb.ToString());
    }

    private void GetCourseTags(HttpResponse response)
    {
        TagCoursesBLL objBLL = new TagCoursesBLL();

        List<Tag_Info> objList = objBLL.GetCourseTags(helper.GetInt("cid"));

        StringBuilder objSb = new StringBuilder();

        objSb.Append("[");
        for (int i = 0; i < objList.Count; i++)
        {
            Tag_Info model = (Tag_Info)objList[i];

            objSb.Append("{\"tid\":\"");
            objSb.Append(model.TagID);
            objSb.Append("\",\"tname\":\"");
            objSb.Append(model.TagName);
            objSb.Append("\"}");

            if (i != objList.Count - 1)
            {
                objSb.Append(",");
            }
        }

        objSb.Append("]");

        response.Write(objSb.ToString());
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}